/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: error_code.h
 *
 * Purpose: This section describes the ara::core::ErrorCode type which 
 *          holds a domainspecific error
 *
 * Developer:
 *   wen.gu , 2020-04-08
 *
 * TODO:
 *
 ***************************************************************************/

#ifndef __ARA_CORE_ERROR_CODE_H__
#define __ARA_CORE_ERROR_CODE_H__
/******************************************************************************
 **    INCLUDES
 ******************************************************************************/

#include <cstdint>
#include "ara/core/error_domain.h"
#include "ara/core/string_view.h"
//#include "ara/core/string.h"
//#include "ara/core/core_error_domain.h"
/******************************************************************************
 **    MACROS
 ******************************************************************************/


/******************************************************************************
 **    TYPE DEFINITIONS
 ******************************************************************************/
namespace ara
{
namespace core
{
/******************************************************************************
 **    CLASSES/FUNCTIONS DEFINITIONS
 ******************************************************************************/

class ErrorCode final
{
public:
    template <typename EnumT, class = typename std::enable_if<std::is_enum<EnumT>::value>::type>
    constexpr ErrorCode(EnumT e, ErrorDomain::SupportDataType data = ErrorDomain::SupportDataType()) noexcept
        :ErrorCode(MakeErrorCode(e, data))
    {
        /** todo something */
    }

    constexpr ErrorCode(ErrorDomain::CodeType value, ErrorDomain const& domain, 
                        ErrorDomain::SupportDataType data = ErrorDomain::SupportDataType()) noexcept
        :mCode(value),
        mSupportData(data),
        mErrDomain(domain)
    {
        /** todo something */
    }

public:
    constexpr ErrorDomain::CodeType Value() const noexcept { return mCode; }
    constexpr ErrorDomain::SupportDataType SupportData() const noexcept { return mSupportData; }

    constexpr ErrorDomain const& Domain() const noexcept { return mErrDomain; }
    StringView Message() const noexcept{ return mErrDomain.Message(mCode); }
public:
    [[noreturn]] void ThrowAsException() const
    {
        mErrDomain.ThrowAsException(*this);
    }
private:
    ErrorDomain::CodeType mCode;
    ErrorDomain::SupportDataType mSupportData;
    const ErrorDomain& mErrDomain;
};

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
constexpr inline bool operator== (ErrorCode const& lhs, ErrorCode const& rhs) noexcept
{
    return (lhs.Domain() == rhs.Domain()) && (lhs.Value() == rhs.Value());
}

constexpr inline bool operator!= (ErrorCode const& lhs, ErrorCode const& rhs) noexcept
{
    return !(lhs == rhs);
}


/** add a global helper template */
template<typename EnumT, class = typename std::enable_if<std::is_enum<EnumT>::value>::type>
constexpr void ThrowExceptionOrExit(EnumT e, ErrorDomain::SupportDataType data = ErrorDomain::SupportDataType())
{
    MakeErrorCode(e, data).ThrowAsException();
}


} /** namespace core */
} /** namespace ara */
#endif /** !__ARA_CORE_ERROR_CODE_H__ */

